#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "../source/Log.hpp"
static size_t Split(std::string &src, std::string &sep, std::vector<std::string> *arry)
{
	// aa,bb,cc,
	int offset = 0;
	while (offset < src.size())
	{
		size_t pos = src.find(sep, offset);
		if (pos == std::string::npos)
		{
			// 没有找到，就说明都是字串
			arry->push_back(src.substr(offset));
			break;
		}
		if (pos == offset)
		{
			offset = pos + sep.size();
			continue;
		}
		arry->push_back(src.substr(offset, pos - offset));
		offset = pos + sep.size();
	}
	return arry->size();
}

static bool ReadFile(std::string &filename, std::string &buff)
{
	std::ifstream ifs(filename, std::ios::binary);
	if (ifs.is_open() == false)
	{
		DEL_LOG("open filename error : %s", filename.c_str());
		return false;
	}
	size_t ret = 0;
	ifs.seekg(0, ifs.end);
	ret = ifs.tellg();
	ifs.seekg(0, ifs.beg);
	buff.resize(ret);
	ifs.read(&buff[0], ret);
	if (ifs.good() == false)
	{
		DEL_LOG("read filename error : %s", filename.c_str());
		ifs.close();
		return false;
	}
	ifs.close();
	return true;
}

static bool WriteFile(std::string &filename, const std::string &buff)
{
	std::ofstream ofs(filename, std::ios::binary);
	if (ofs.is_open() == false) // 打开文件
	{
		DEL_LOG("open filename error : %s", filename.c_str());
		return false;
	}
	ofs.write(buff.c_str(), buff.size());
	if (ofs.good() == false) // 判断上一步是否成功
	{
		DEL_LOG("write filename error : %s", filename.c_str());
		ofs.close();
		return false;
	}
	ofs.close();
	return true;
}

static std::string URLEncode(const std::string &url, bool convert_space_to_plus)
{
	std::string res;
	for (auto &e : url)
	{
		if (e == '.' || e == '-' || e == '_' || e == '~')
		{
			res += e;
			continue;
		}
		if (e == ' ' && convert_space_to_plus)
		{
			res += '+';
			continue;
		}
		// 剩下的字符都是需要编码成为 %HH 格式
		char tmp[4];
		snprintf(tmp, 4, "%%%02X", e);
		res += tmp;
	}
	return res;
}

static char HexToi(char c)
{
	if (c >= '0' && c <= '9')
	{
		return c - '0';
	}
	else if (c >= 'a' && c <= 'z')
	{
		return c - 'a' + 10;
	}
	else if(c >= 'A' && c <= 'Z')
	{
		return c - 'A' + 10;
	}
	return -1;
}
// URL解码
static std::string URLDecode(const std::string &url, bool convert_space_to_plus)
{
	std::string res;
	// 遇到了%，则将紧随其后的2个字符，转换为数字，第一个数字左移4位，然后加上第二个数字  + -> 2b  %2b->2 << 4 + 11
	for (int i = 0; i < url.size(); i++)
	{
		if (url[i] == '+' && convert_space_to_plus)
		{
			res += ' ';
			continue;
		}
		if (url[i] == '%')
		{
			char v1 = HexToi(url[i + 1]);
			char v2 = HexToi(url[i + 2]);
			char v = (v1 << 4) + v2;
			res += v;
			i += 2;
		}
	}
	return res;
}

int main()
{
	std::string url = "C++  ";
	std::string ret = URLEncode(url, true);
	std::cout << ret << std::endl;
	std::string rset = URLDecode(ret, true);

	std::cout << rset << std::endl;

	// std::string filename = "main.cc";
	// std::string buff;
	// ReadFile(filename, buff);
	// std::cout << buff << std::endl;
	// std::string filename1 = "main1111.cc";
	// WriteFile(filename1, buff);
	// std::string src = "aa,,,,bb,cc,";
	// std::string sep = ",";
	// std::vector<std::string> arry;
	// size_t ret = Split(src, sep, &arry);
	// for (auto &e : arry)
	// {
	// 	std::cout << "[" << e << "]" << std::endl;
	// }
	return 0;
}
